#pragma once

#include "parentpanel.h"

class DividedPanel : public ParentPanel
{
protected:
	bool m_separationHorizontal;
	static const int MAX_CHILDREN = 2;
	static const int NO_SEPERATOR = -1;
	static const int DEFAULT_PIXEL_SEPARATION = 6;
	int m_seperatorSize;
	int m_movingSeperator;
	SRect m_seperator;
public:
	DividedPanel():m_separationHorizontal(true),m_seperatorSize(DEFAULT_PIXEL_SEPARATION),
		m_movingSeperator(NO_SEPERATOR)
	{}
	void setSeperationHorizontal(const bool & a_separationHorizontal)
	{m_separationHorizontal = a_separationHorizontal;}
	void divideAreaForChildren()
	{
		int numDivisions = (m_children.size()-1);
		if(numDivisions > 0 && getArea()->isValid())
		{
			SRect childArea = *getArea();
			int dividedDimension = m_separationHorizontal?SRect::X:SRect::Y;
			int overheadAmount = m_seperatorSize;
			overheadAmount *= numDivisions;
			int initialAmount = childArea.getField(SRect::DIMENSION+dividedDimension)-overheadAmount;
			int dividedAmount = initialAmount/(numDivisions+1);
			childArea.setField(SRect::DIMENSION+dividedDimension, dividedAmount);
			int currentLocation = childArea.getField(dividedDimension);
			for(int i = 0; i < m_children.size(); ++i)
			{
				m_children.get(i)->setArea(childArea);
				currentLocation += dividedAmount+m_seperatorSize;
				childArea.setField(dividedDimension, currentLocation);
			}
		}
	}
	virtual void setArea(const SRect & a_dimension)
	{
		ParentPanel::setArea(a_dimension);
		divideAreaForChildren();
	}
	bool addChild(Panel * p)
	{
		bool added = false;
		if(m_children.size() < MAX_CHILDREN)
		{
			ParentPanel::addChild(p);
			divideAreaForChildren();
			added = true;
		}
		return added;
	}
	void draw(SDL_Surface * a_surface)
	{
		m_seperator.set(*getArea());
		int dividedDimension = m_separationHorizontal?SRect::X:SRect::Y;
		m_seperator.setField(SRect::DIMENSION+dividedDimension, m_seperatorSize);
		for(int i = 0; i < m_children.size()-1; ++i)
		{
			m_seperator.setField(dividedDimension, 
				m_children.get(i)->getArea()->getField(SRect::MAX+dividedDimension));
			VSDL_drawRect(a_surface, &m_seperator, 0, 1, m_separationHorizontal
				?(DRAW_RECT_MINX_SIDE|DRAW_RECT_MAXX_SIDE)
				:(DRAW_RECT_MINY_SIDE|DRAW_RECT_MAXY_SIDE));
		}
		//SRect childSpot;
		//for(int i = 0; i < m_children.size(); ++i)
		//{
		//	childSpot.set(*m_children.get(i)->getArea());
		//	VSDL_FillRect(a_surface, &childSpot, -1);
		//}
		if(m_movingSeperator != NO_SEPERATOR)
		{
			VSDL_FillRect(a_surface, &m_seperator, -1);
		}
		//else
		{
			ParentPanel::draw(a_surface);
		}
	}
	void setSeperatorAt(const SPoint & a_mouse)
	{
		m_seperator.set(*getArea());
		int dividedDimension = m_separationHorizontal?SRect::X:SRect::Y;
		m_seperator.setField(SRect::DIMENSION+dividedDimension, m_seperatorSize);
		m_seperator.setField(dividedDimension, a_mouse.getField(dividedDimension)-m_seperatorSize/2);
	}
	bool handleEvent(const SDL_Event & e)
	{
		bool handled = false;
		switch(e.type)
		{
		case SDL_MOUSEBUTTONDOWN:{
			SPoint mouse(e.button.x, e.button.y);
			int clickedSeperation = NO_SEPERATOR;
			m_seperator.set(*getArea());
			int dividedDimension = m_separationHorizontal?SRect::X:SRect::Y;
			m_seperator.setField(SRect::DIMENSION+dividedDimension, m_seperatorSize);
			for(int i = 0; clickedSeperation < 0 && i < m_children.size()-1; ++i)
			{
				m_seperator.setField(dividedDimension, 
					m_children.get(i)->getArea()->getField(SRect::MAX+dividedDimension));
				if(m_seperator.contains(mouse))
					clickedSeperation = i;
			}
			if(clickedSeperation != NO_SEPERATOR)
			{
				m_movingSeperator = clickedSeperation;
				printf("clicked seperation %d!\n", clickedSeperation);
				handled = true;
				setSeperatorAt(mouse);
				setDirty(0);
			}
			break;}
		case SDL_MOUSEMOTION:
			if(m_movingSeperator != NO_SEPERATOR)
			{
				setSeperatorAt(SPoint(e.motion.x, e.motion.y));
				setDirty(0);
				m_parent->setDirty(0);
				handled = true;
			}
			break;
		case SDL_MOUSEBUTTONUP:
			if(m_movingSeperator != NO_SEPERATOR)
			{
				SPoint mouse(e.button.x, e.button.y);
				int dividedDimension = m_separationHorizontal?SRect::X:SRect::Y;
				// set first child
				SRect area(*m_children.get(m_movingSeperator)->getArea());
				area.setField(SRect::MAX+dividedDimension, mouse.getField(dividedDimension)-m_seperatorSize/2);
				m_children.get(m_movingSeperator)->setArea(area);
				// set second child
				area.set(*m_children.get(m_movingSeperator+1)->getArea());
				area.setField(SRect::MIN+dividedDimension, mouse.getField(dividedDimension)+m_seperatorSize/2);
				m_children.get(m_movingSeperator+1)->setArea(area);
				// all done!
				m_movingSeperator = NO_SEPERATOR;
				handled = true;
				setDirty(0);
			}
			break;
		}
		if(!handled)
			handled = ParentPanel::handleEvent(e);
		return handled;
	}
};